package com.phospherus.customerinfo.service.impl;

import com.google.gson.Gson;
import com.phospherus.common.constant.Constant;
import com.phospherus.common.service.impl.BaseServiceImpl;
import com.phospherus.customerinfo.entity.BaseMessage;
import com.phospherus.customerinfo.entity.CompanyBaseInfo;
import com.phospherus.customerinfo.repostory.CompanyBaseInfoRepository;
import com.phospherus.customerinfo.service.IQichachaCompanyInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Created by lipeng on 17/11/15
 */
@Service
public class QichachaCompanyInfoServiceImpl extends BaseServiceImpl<CompanyBaseInfo,Long,CompanyBaseInfoRepository>
implements IQichachaCompanyInfoService
{
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 根据公司详细名字查询公司基本信息，若MySQL中已经存在，则返回序列化后的json，若MySQL中无相关公司信息
     * 则通过企查查API进行查询，若查询成功则返回公司信息的json，若查询失败则返回错误信息，查询成功后将公司信息写入到MySQL中
     * @param keyword 公司的详细名称
     * @return
     * @throws Exception
     */
    @Override
    public  BaseMessage getDetailsByName(String keyword) throws Exception{
        CompanyBaseInfo baseInfo = null;
        BaseMessage message = null;
        try {
            //通过公司名字获取公司基本信息
            baseInfo  = this.findByName(keyword);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        Gson json = new Gson();
        if(baseInfo==null){
            //若数据库中不存在，则调用接口，并格式化后存入到数据库中
            try {
                String json_message = restTemplate.getForObject(Constant.BASE_URL+Constant.API_GETDETAILSBYNAME+keyword+Constant.API_TOKEN_KEY_STRING,String.class);
                if(StringUtils.isNotEmpty(json_message)){
                    message =  json.fromJson(json_message, BaseMessage.class);
                    if(Constant.REST_API_STATUS.equals(message.getStatus())) {
                        Map map = message.getResult();
                        try {
                            baseInfo = new CompanyBaseInfo(message.getResult());
                            if(this.findByName(baseInfo.getName())==null){
                                this.save(baseInfo);
                            }
                        }catch (Exception ex){
                            ex.printStackTrace();
                        }
                    }
                }
            }catch (Exception ex){
                //处理过程中若有异常返回基础错误信息
                message = new BaseMessage();
                message.setMessage(Constant.BASE_ERROR_MSG);
            }
        }else {
            message = new BaseMessage();
            message.setStatus(Constant.REST_API_STATUS);
            message.setResult(json.fromJson(json.toJson(baseInfo),Map.class));
        }
        return message;
    }



    @Override
    public CompanyBaseInfo findByName(String name) {
        return repository.findByName(name);
    }
}
